# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import win
import curses

class MsgWin(win.Win):
	def __init__(self, mam, title = '', msg = [], buttons = [], width = 50):
		self.title = title
		self.msg = msg
		self.buttons = buttons
		self.buttonIndex = self.defaultButIndex()
		self.mam = mam
		self.mam.ui.options_win = 1
		self.height = self.calculateHeight()
		self.width = width
		# Creo la ventana
		win.Win.__init__(self, self.height, self.width, (self.mam.ui.maxY-self.height)/2, (self.mam.ui.maxX-self.width)/2)
		# Muestro la ventana
		self.show()
		self.mam.ui.winOpt2 = self

	def lenButtons(self):
		count = 0
		for b in self.buttons:
			count += len(b.descrip) + 2
		return count

	def defaultButIndex(self):
		default = 0
		for but in self.buttons:
			if but.default:
				default = self.buttons.index(but)
				break
		return default

	def show(self):
		self.w.bkgd(self.mam.ui.COLORS.OPTIONS_WIN1)
		# Creo el borde
		self.mam.ui.rectangle(self.w, 0, 0, self.height - 1, self.width - 1)
		# Muestro title
		self.move(1,3)
		self.addstr(self.title, self.mam.ui.COLORS.OPTIONS_WIN2)
		# Muestro mensaje
		i = 3
		lineChars = 0
		for line in self.msg:
			if lineChars:
				lineChars += 1
			self.move(i, max(lineChars + (self.width - lineChars - len(line.text))/2, 3))
			self.addstr(line.text[:self.width-6], line.color)
			if line.nl: 
				i += 1
				lineChars = 0
			else:
				lineChars += len(line.text) + 1
		self.refresh()

	def refresh(self):
		# Muestro botones
		colorDef = self.mam.ui.COLORS.OPTIONS_WIN5
		colorNotDef = self.mam.ui.COLORS.OPTIONS_WIN1
		numBut = 0
		for but in self.buttons:
			self.move(self.height - 2, (self.width-14)/2 + numBut*9)
			numBut += 1
			if self.buttons.index(but) == self.buttonIndex:
				self.addstr(" " + but.descrip + " ", colorDef)
			else:
				self.addstr(" " + but.descrip + " ", colorNotDef)
		# Refresh de ventana
		self.w.refresh()

	def rebuildWin(self):
		self.show()

	def calculateHeight(self):
		height = 6
		for line in self.msg:
			if line.nl: 
				height += 1
		return height

	def manageInput(self, carac):
		# F4
		if carac == int(0x10c):
			return -1
		# ENTER
		elif carac == int(0xa):
			return self.buttonIndex							      
		# TAB / KEY_DOWN / KEY_UP / KEY_LEFT / KEY_RIGHT
		elif carac in [int(0x9), curses.KEY_DOWN, curses.KEY_UP, curses.KEY_LEFT, curses.KEY_RIGHT]:
			self.buttonIndex += 1
			if self.buttonIndex == len(self.buttons):
				self.buttonIndex = 0
			self.refresh()
		return -2
